package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.CustomException;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.FacilitiesCategoryMapper;
import com.qf.agro.mapper.FacilitiesMapper;
import com.qf.agro.mapper.SysUserMapper;
import com.qf.agro.pojo.Facilities;
import com.qf.agro.pojo.FacilitiesCategory;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.FacilitiesService;
import com.qf.agro.vo.OptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/7/3 20:29
 */
@Service
public class FacilitiesServiceImpl implements FacilitiesService {


    @Autowired
    private FacilitiesMapper facilitiesMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private FacilitiesCategoryMapper facilitiesCategoryMapper;
    @Override
    public ResultInfo getPage(Long page, Long pageSize, String name) {
        Page<Facilities> pages=new Page<>(page,pageSize);
        LambdaQueryWrapper<Facilities> lqw=new LambdaQueryWrapper<>();
        SysUser sysUser = SysUserHolder.get();
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getId, sysUser.getId());
        sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
        lqw.like(name!=null,Facilities::getFacilitiesName,name);
         lqw.eq(Facilities::getCompanyId,sysUser.getCompanyId());
        pages= facilitiesMapper.selectPage(pages, lqw);

        return ResultInfo.success(pages);
    }

    @Override
    public ResultInfo save(Facilities facilities) {
        int insert = facilitiesMapper.insert(facilities);
        return insert>0?ResultInfo.success("添加成功"):ResultInfo.error("添加失败");
    }

    @Override
    public ResultInfo deleteById(Long id) {

        facilitiesMapper.deleteById(id);
        return ResultInfo.success("删除成功");
    }

    @Override
    public ResultInfo update(Facilities facilities) {
        facilitiesMapper.updateById(facilities);
        return ResultInfo.success("添加成功");
    }

    @Override
    public ResultInfo getOptions() {

        List<OptionVo> optionVos=facilitiesCategoryMapper.getOptions();
        System.out.println(optionVos);
        return ResultInfo.success(optionVos);
    }
}
